package fr.umlv.cmis.treeview;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

import kiyut.ekspos.BrowserWindow;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.commons.enums.BaseTypeId;

// TODO: Auto-generated Javadoc
/**
 * The Class CmisObjectTreeNode.
 */
public class CmisObjectTreeNode extends DefaultMutableTreeNode {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	/** The cmis object. */
	private CmisObject cmisObject ;
	
	/** The parent. */
	private TreeNode parent;
	
	/** The children. */
	private List<CmisObjectTreeNode> children = null;
	
	/** The is dir. */
	private boolean isDir;


	/**
	 * Instantiates a new cmis object tree node.
	 *
	 * @param cmisObject the cmis object
	 * @param parent the parent
	 */
	public CmisObjectTreeNode(CmisObject cmisObject, TreeNode parent) {

		this.cmisObject = cmisObject;
		this.parent=parent;
		isDir = BaseTypeId.CMIS_FOLDER.equals(cmisObject.getBaseTypeId());
	}

	/**
	 * Gets the cmis object.
	 *
	 * @return the cmis object
	 */
	public CmisObject getCmisObject() {
		return cmisObject;
	}

	/**
	 * Gets the children.
	 *
	 * @return the children
	 */
	public List<CmisObjectTreeNode> getChildren() {

		if (children!=null) 
			return children;

		children = new ArrayList<CmisObjectTreeNode>();

		Folder folder = (Folder) cmisObject;

		if (folder.getChildren().getTotalNumItems()==0) 
			return children;

		ItemIterable<CmisObject> iterable  = folder.getChildren();
		Iterator<CmisObject> iterator = iterable.iterator();
		
		while(iterator.hasNext()){
			CmisObject cmisobj = iterator.next();
			if(BaseTypeId.CMIS_FOLDER.equals(cmisobj.getBaseTypeId()))
				if(!cmisobj.getName().equals(".gedconfig")) {
					if(!BrowserWindow.getBlacklist().contains( ((Folder)cmisobj).getPath())) {
						children.add(new CmisObjectTreeNode(cmisobj, this));
					}
				}
		}

		Collections.sort(children, new CmisObjectTreeNodeNameComparator());
		return children;

	}


	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#children()
	 */
	public Enumeration<CmisObjectTreeNode> children() {

		return Collections.enumeration(getChildren());
	}


	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#getAllowsChildren()
	 */
	public boolean getAllowsChildren() {
		return isDir;
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#getChildAt(int)
	 */
	public TreeNode getChildAt(int childIndex) {

		return getChildren().get(childIndex);
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#getChildCount()
	 */
	public int getChildCount() {

		return getChildren().size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#getIndex(javax.swing.tree.TreeNode)
	 */
	public int getIndex(TreeNode node) {

		return getChildren().indexOf(node);
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#getParent()
	 */
	public TreeNode getParent() {
		return parent;
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#toString()
	 */
	@Override
	public String toString() {
		return cmisObject.getName();
	}

	/* (non-Javadoc)
	 * @see javax.swing.tree.DefaultMutableTreeNode#isLeaf()
	 */
	public boolean isLeaf() {
		return !isDir;
	}

	/**
	 * Sets the cmis object.
	 *
	 * @param cmisObject the new cmis object
	 */
	public void setCmisObject(CmisObject cmisObject) {
		this.cmisObject = cmisObject;
	}

	/**
	 * The Class CmisObjectTreeNodeNameComparator.
	 */
	private class CmisObjectTreeNodeNameComparator implements Comparator<CmisObjectTreeNode> {
		
		/* (non-Javadoc)
		 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
		 */
		public int compare(CmisObjectTreeNode file1, CmisObjectTreeNode file2) {
			return file1.getCmisObject().getName().toLowerCase().compareTo(file2.getCmisObject().getName().toLowerCase());
		}
	}

}
